function [ output_args ] = generate_kml(G, indir, outdir, participant, session)

disp(['Generating kml for participant ' int2str(participant) ' session ' int2str(session)]);
m = load([G.DIR.DATA G.DIR.SEP indir G.DIR.SEP 'p' int2str(participant) '_s' num2str(session,'%.2d') '_frmtdata']);
fpKml = fopen([G.DIR.DATA G.DIR.SEP outdir G.DIR.SEP 'Participant ' int2str(participant) ' Session ' int2str(session) '.kml'], 'w');

skeletonPath = 'functions/kml/skleton_kml.txt';
skeletonKml = getFileContent(skeletonPath);
skeletonKmlParts = regexp(skeletonKml,'#PUT_CONTENT_HERE#', 'split');
skeletonKmlHeader = char(skeletonKmlParts(1));
skeletonKmlFooter = char(skeletonKmlParts(2));

fprintf(fpKml, '%s', skeletonKmlHeader);

kmlCreateFolder(fpKml, ['Participant ' int2str(participant) ' Session ' int2str(session)], 1);
	kmlCreateFolder(fpKml, 'gps', 0);
	
	kmlGeneratePath(fpKml, G, ...
		m.D.gps{G.SENSOR.P_GPS_LATID}.sample, ...
		m.D.gps{G.SENSOR.P_GPS_LONGID}.sample, ...
		m.D.gps{G.SENSOR.P_GPS_ALTID}.sample, ...
		m.D.gps{G.SENSOR.P_GPS_SPDID}.sample, ...
		m.D.gps{G.SENSOR.P_GPS_ACCURACYID}.sample, ...
		m.D.gps{G.SENSOR.P_GPS_LATID}.timestamp);
	
	kmlCloseFolder(fpKml);

	kmlCreateFolder(fpKml, 'ema', 1);
	[rowCount, colCount] = size(m.D.ema.data);
	for emaIndex=1:colCount
		try
			ema = m.D.ema.data(emaIndex).question(12).response;
			[timeDiff, index] = min(abs(m.D.gps{G.SENSOR.P_GPS_LATID}.timestamp - m.D.ema.data(emaIndex).question(12).timestamp));
			label = '';
			switch ema
				case 0
					%There is no ema for this.
					continue;
				case 1
					label = 'Home';
				case 2
					label = 'Work';
				case 3
					label = 'Store';
				case 4
					label = 'Restaurant';
				case 5
					label = 'Vehicle';
				case 6
					label = 'Outside';
				case 7
					label = 'Other';
			end
			kmlGeneratePoint(fpKml, G, label, ...
				m.D.gps{G.SENSOR.P_GPS_LATID}.sample(index), ...
				m.D.gps{G.SENSOR.P_GPS_LONGID}.sample(index), ...
				m.D.gps{G.SENSOR.P_GPS_ALTID}.sample(index), ...
				m.D.gps{G.SENSOR.P_GPS_SPDID}.sample(index), ...
				m.D.gps{G.SENSOR.P_GPS_ACCURACYID}.sample(index), ...
				m.D.gps{G.SENSOR.P_GPS_LATID}.timestamp(index), ...
				m.D.ema.data(emaIndex).question(12).timestamp ...
				);
		catch e
		end
	end
	%m.D.ema.data(1).question(12).response
	kmlCloseFolder(fpKml);
kmlCloseFolder(fpKml);

fprintf(fpKml, '%s', skeletonKmlFooter);
fclose(fpKml);

end

